<?php

namespace App\Admin\Controllers;

use App\Models\GoldProduct;
use Carbon\Carbon;
use Encore\Admin\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\Product;
use App\Models\GoldSeed;
use App\Models\GoldRound;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\GoldSeedAction\GoldSeedSplits;
use App\Admin\Actions\GoldSeedAction\BatchGoldProducts;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Log;

class GoldSeedsController extends AdminController
{
	use ValidatesRequests;

    protected $title = '金店商品种子';

    protected function grid()
    {
        $grid = new Grid(new GoldSeed());
        $grid->model()->orderByDesc('id');

        $grid->column('id', 'ID');
        $grid->column('title', '标题');
	    $grid->column('product_id', '商品')->display(function () {
		    return $this->product->id.":".$this->product->title;
	    });
        $grid->column('all_rounds', '轮次');
        $grid->column('init_price', '发行价');
        $grid->column('split_gold_round_id', '拆单场次')->display(function ($value) {
            return $value ? $this->splitGoldRound->title : '';
        });
        $grid->column('split_amount', '拆单数量');
	    $grid->column('created_at', '创建')->display(function () {
		    return (string)$this->created_at;
	    });

	    $grid->disableRowSelector();
	    $grid->disableExport();
	    $this->gridActions($grid);

        return $grid;
    }

	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
			$actions->add(new BatchGoldProducts());
		});
	}

    protected function form()
    {
        $form = new Form(new GoldSeed());

	    $form->text('title', '商品种子标题')->rules('required|string|unique:gold_seeds,title,{{id}}');

	    $form->select('product_id', '种子商品来源')->options(Product::all()->pluck('id', 'id'));

	    $form->number('all_rounds', '金店轮次')->default(6)->rules('required|min:1');

	    $form->currency('init_price', '首次发行价格')->symbol('￥')->default(0.00)->rules('required|min:0.01');
	    $form->currency('split_price', '拆单最终价格')->symbol('￥')->default(300.00)->rules('required|min:0.01');

        $form->select('split_gold_round_id', '末轮拆单场次')
            ->options(GoldRound::all()->pluck('title', 'id'))
            ->rules('required');
        $form->select('normal_gold_seed_id', '基础拆单种子')
            ->options(GoldSeed::all()->pluck('title', 'id'));

        $form->number('split_amount', '末轮拆单数量')->default(3)->rules('required|min:1');

        $form->select('transfer_gold_seed_id', '末轮置换种子')
            ->options(GoldSeed::all()->pluck('title', 'id'));
        $form->rate('transfer_rate', '末轮置换比例')->default(80)->rules('min:0|max:100');
        $form->select('transfer_gold_round_id', '末轮置换场次')
            ->options(GoldRound::all()->pluck('title', 'id'));

	    $this->formTools($form);
	    $this->formSaving($form);

        return $form;
    }

	protected function formTools(Form $form)
	{
		$form->tools(function (Form\Tools $tools) {
			$tools->disableView();
			$tools->disableDelete();
		});
	}

	protected function formSaving(Form $form)
	{
		$form->saving(function (Form $form) {
			/*if (has_locking_gold_round()
				and $form->isEditing()) {
				admin_error('金店金店活动中...不可编辑!');
				return redirect('admin/gold-seeds');
			}*/
			return true;
		});
	}

	public function batchGoldProductsFrom(Content $content)
	{
		$sid = request('sid');

		if (!$seed = GoldSeed::find($sid)) {
			admin_toastr('金店种子不存在', 'error');
			return redirect('/admin/gold-seeds');
		}

		$form = new \Encore\Admin\Widgets\Form();

		$form->action(route('admin.gold.seeds.batch.products', ['gold_seed_id' => $sid]));

		$form->html("<b>种子：</b>{$seed->title}");

		$form->select('gold_round_id', '场次')
			->options(GoldRound::all()->pluck('title', 'id'))
			->rules('required');

		$form->text('amount', '数量')->rules('required');

		$box = (new Box('种子批量生成金店商品', $form))->style('info');

		return $content->header('批量生成种子商品')->body($box);
	}

	public function storeBatchGoldProducts()
	{
		if (! GoldSeed::find(request('gold_seed_id'))) {
			admin_toastr('金店种子不存在', 'error');
			return redirect('/admin/gold-seeds');
		}

		$data = $this->validate(request(), [
			'gold_seed_id' => 'required|integer:min:1',
			'gold_round_id' => 'required|integer:min:1',
			'amount' => 'required|integer|min:0|max:500',
		], [], [
			'gold_seed_id' => '种子',
			'gold_round_id' => '场次',
			'amount' => '数量',
		]);

		for ($i = 0; $i < $data['amount']; $i++) {
			GoldProduct::create([
				'gold_seed_id' => $data['gold_seed_id'],
				'gold_round_id' => $data['gold_round_id'],
                'started_at' => Carbon::now()
			]);
		}

		admin_toastr('批量创建成功', 'success');

		return redirect('/admin/gold-seeds');
	}
}
